package com.neu.api.controller.admin;


import com.neu.api.constan.StatusConst;
import com.neu.api.dto.UserDTO;
import com.neu.api.exception.ParamException;
import com.neu.api.properties.JWTProperties;
import com.neu.api.query.UserQuery;
import com.neu.api.service.UserService;
import com.neu.api.utils.JWTUtil;
import com.neu.api.utils.PageResult;
import com.neu.api.utils.Result;
import com.neu.api.utils.StringUtil;
import com.neu.api.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户接口")
@RestController
@RequestMapping("admin/user")
@Slf4j
public class UserController {

    Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    JWTProperties jwtProperties;
//    @Value("${neu.jwt.secure}")
//    private String secure;
    //vo dto pojo
              //
    @Resource
    UserService userService;

    /**
     * 用户登录
     * @param loginName
     * @param password
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<List<UserVO>>Login(String loginName,String password){
        log.info("用户登录："+"账号："+loginName+",密码："+password);
        //参数的校验
        if(StringUtil.isEmpty(loginName) || StringUtil.isEmpty(password)){
            throw new ParamException("账号和密码都不能为空");
        }

        UserVO userVO = userService.login(loginName,password);
        if(userVO==null){
            return Result.failure("账号或密码错误");
        }

        if(userVO.getStatus()== StatusConst.INACTIVE_STATUS){
            return Result.failure("账号被冻结");
        }
        //UserVO userVO = userService.login(loginName,password);

        //服务器生成token --> 响应给前端----->前端每次发请求都携带token------->服务器要校验

        Map<String,Object> payload = new HashMap<>();
        payload.put(JWTUtil.USER_ID,userVO.getId());

        String token = JWTUtil.generateToken(payload,jwtProperties.getSecure(),jwtProperties.getExpire());
        log.info("token："+token);
        Map<String,Object> data = new HashMap<>();
        data.put("token",token);
        data.put("userInfo",userVO);
        return Result.ok("成功",data);
    }


    @ApiOperation("查询用户接口")
    @GetMapping
    public Result<List<UserVO>> find(UserQuery userQuery){
        logger.info("调用了find方法，查询用户");
        PageResult<UserVO> page = userService.find(userQuery);
        return Result.ok("成功",page.getList(),page.getTotal());
    }

    /**
     * 添加用户信息
     * @param userDTO
     * @return
     */
    @ApiOperation("添加用户接口")
    @PostMapping
    public Result add(UserDTO userDTO){
        log.info("添加信息参数为："+userDTO.toString());
        userService.add(userDTO);
        return Result.ok("添加成功");
    }

    /**
     * 编辑用户信息
     * @param id
     * @param userDTO
     * @return
     */
    @ApiOperation("编辑用户")
    @PutMapping("/{id}")
    public Result update(@PathVariable long id,UserDTO userDTO){
        log.info("编辑当前用户id为："+id+",参数为："+userDTO.toString());
        userService.update(id,userDTO);
        return Result.ok("修改成功！");
    }

    /**
     * 根据ID删除用户
     * @param id
     * @return
     */
    @ApiOperation("根据ID删除用户")
    @DeleteMapping("/{id}")
    public Result update(@PathVariable long id){
        log.info("删除用户的id位："+id);
        userService.deleteById(id);
        return Result.ok("删除成功！");
    }

    /**
     * 启用禁用账号
     * @param id
     * @return
     */
    @ApiOperation("启用禁用账号")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable long id,String status){
        log.info("启用禁用账号id:"+id+"，状态为："+status);
//        byte nowStatus = userService.updateStatus(id,status);
//        String msg = nowStatus==1||nowStatus==0?"账号禁用":"账号启用";
//        return Result.ok(msg);
        if(StatusConst.INACTIVE_STATUS_NAME.equals(status)){
            userService.updateStatus(id,StatusConst.INACTIVE_STATUS);
        }else if(StatusConst.ACTIVE_STATUS_NAME.equals(status)){
            userService.updateStatus(id,StatusConst.ACTIVE_STATUS);
        }else{
            throw new ParamException("参数错误");
        }
        return Result.ok("修改状态成功");
    }
}
